<?php

namespace app\common\service\ai;

use think\facade\Db;
use app\common\service\CommonService;

/**
 * 系统配置
 */
class SystemConfigService extends CommonService {
    
    /**
     * 表名
     * @static string
     */
    protected static $table = 'system_config';

    const CACHE_KEY = 'system_config';
    
    /**
     * 列表
     * @param string $key 
     * @param string $filed 
     * @return array
     */
    public static function getList($key = '', $filed = 'id,value,title,status,extra') {
        $query = self::query(self::$table);
        if ($key && stripos($key, ',') !== false) {
            $query->whereIn('name', explode(',', $key));
        }
        return $query->_select($filed);
    }

    /**
     * 详情
     * @param string $key
     * @param string $filed
     * @return array
     */
    public static function getDetail($key = '', $filed = 'id,value,title,status,extra') {
        return self::query(self::$table)->where(['name' => $key])->_find($filed);
    }
    
    /**
     * 编辑
     * @param array $data
     * @return mixed
     */
    public static function edit($data) {
        return self::model()->_update(self::$table, $data, self::ID($data));
    }

    /**
     * 列表
     * @param int $group
     * @param string $field
     * @return array
     */
    public static function getListByGroup($group = 0, $field = '*') {
        $query = self::query(self::$table)->where(['status' => 1]);
        $group && $query->where(['group' => $group]);
        return $query->order('sort desc')->_select($field);
    }

    /**
     * 分组
     * @return array
     */
    public static function getAllGroup() {
        return array(
//            '1' => '网站配置',
//            '2' => '系统配置',
			'3' => '其他配置'
        );
    }

    /**
     * 指定键值
     * @staticvar static $config
     * @param string $key
     * @return mixed
     */
    public static function get($key = NULL) {
        static $config = NULL;
        if (is_null($config)) {
            // 从缓存读取
            $config = self::getFromCache();
            if (empty($config)) { // 从数据库读取
                $config = self::getFromDb();
                self::set($config); // 将配置写入缓存
            } else {
                $config = json_decode($config, true);
            }
        }
        return is_null($key) ? $config : ($config[$key] ?? NULL);
    }

    /**
     * cache读取
     * @return mixed
     */
    public static function getFromCache() {
        return C(self::CACHE_KEY);
    }

    /**
     * db读取
     * @return mixed
     */
    public static function getFromDb() {
        $list = self::getListByGroup();
        $config = array();
        foreach ($list as $row) {
            $config[$row['name']] = $row['type'] == 3 ? explode(',', $row['value']) : $row['value'];
        }
        return $config;
    }

    /**
     * 设置
     * @param string $key
     * @param string $value
     * @return boolean
     */
    public static function set($key, $value = '') {
        $config = self::get();
        if (is_array($key)) {
            foreach ($key as $k => $val) {
                $config[$k] = $val;
            }
        } else {
            $config[$key] = $value;
        }
        C(self::CACHE_KEY, json_encode($config));
        return true;
    }

    /**
     * 保存
     * @param array $data
     * @return boolean
     */
    public static function save($data) {
        foreach ($data as $key => &$value) {
            is_array($value) && $value = join(',', $value);
            if (strtoupper($key) === 'AGREEMENT_CONTENT'){
                $value = htmlspecialchars($value);
            }
            if (self::model()->_update(self::$table, ['value' => $value], ['name' => $key]) === false) {
                return false;
            }
        }
        return self::set($data);
    }

    /**
     * 刷新缓存
     * @return type
     */
    public static function refresh() {
        return C(self::CACHE_KEY, NULL);
    }

}
